﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;

using FCNS.KeepMoney.Data;

namespace FCNS.KeepMoney.Data
{
    /// <summary>
    /// 运算中心,所有的数据添加,移除,编辑要发送到此进行计算
    /// </summary>
    class CalcData
    {
        //public static void Remove(RegularEntryField field)
        //{
        //    //如果用 Program 的值会报错且跨年会导致数据删不全
        //    foreach (EntryField ef in EntryField.GetEntryField.FindAll(gef =>
        //    {
        //        return
        //            gef.Mark == field.Id.ToString();
        //    }))
        //        Remove(ef);

        //    field.Remove();
        //}

        //public static void Remove(GroupField field)
        //{

        //    field.Remove();
        //}

        //public static void Remove(BankField field)
        //{
        //    List<DataRow> rows = new List<DataRow>(KMData.DataTableEntrys.Select("BankId = " + field.Id));
        //    foreach (DataRow row in rows)
        //        KMData.DataTableEntrys.Rows.Remove(row);

        //    field.Remove();
        //}

        //public static void Remove(EntryField field)
        //{
        //    BankField.SetBankBalance(field);
        //    field.Remove();
        //}

        public static void Add(EntryField field)
        {
        }


        private static void CalcRegular(RegularEntryField field)
        {
            if (field.Finish==field.columnTime)
                return;

            BankField bank = BankField.GetBankField.Find(bf => { return bf.Id == field.BankId; });
            Debug.Assert(bank != null);

            int month = -field.Finish;//已经有记录的月份就不要统计啦
            int day = field.columnDay, temp = field.columnTime - field.Finish;

            //计算有多少个月的数据没有添加(防止长时间不登陆软件导致数据不添加)
            DateTime date = field.columnStartDate;
            int nm = DateTime.Now.Year * 12 + DateTime.Now.Month;
            while ((date.Year * 12 + date.Month) < nm)
            {
                date = date.AddMonths(1);
                month++;
            }

            if (DateTime.Now.Day < day)
                month--;

            if (month == 0)
                return;

            if (month > temp)
                month = temp;

            date = field.columnStartDate;
            decimal d =Math.Round( field.columnMoney / field.columnTime + CalcInterest(field),Program.AppConfig.Money.DecimalPlaces);

            for (int i = 1; i <= month; i++)//因为次月生效,从 1 开始索引
            {
                int n = field.Finish + 1;

                EntryField entryField = new EntryField(field.GroupId);
                entryField.columnName = field.columnName;
                entryField.BankId = field.BankId;
                entryField.columnNote = "第" + n.ToString() + "期";
                entryField.Date = Util.DateTimeUtil.yyyyMMdd(date.Year, date.AddMonths(n).Month, day);
                entryField.Money = d;
                entryField.EntryType = KMDEntryType.Regular;
                entryField.Mark = field.Id.ToString();
                entryField.AddRowToDataTable();

                bank.SetBankBalance(entryField.Money);

                field.Finish = n;
            }
        }

        public static void CalcRegular()
        {
            foreach (RegularEntryField field in RegularEntryField.GetRegularEntryField)
                CalcRegular(field);
        }

        private static decimal CalcInterest(RegularEntryField field)
        {
            decimal de = 0;
            InterestField interest =new InterestField( KMData.DataTableInterest.Select("Id = " + field.InterestId)[0]);
          
            switch (interest.Mode)
            {
                case 0:
                    break;

                case 1:
                    break;

                case 2:
                    if (interest.Value > 1)
                        de = interest.Value;
                    else
                        de = field.columnMoney * interest.Value;
                    break;

                case 3:
                    break;
            }

            return de;
        }

        //private static void SetBankBalance(EntryField field)
        //{
        //    BankField bf = BankField.GetBankField.Find(gbf => { return gbf.Id == field.BankId; });
        //    if (field.Income)
        //        bf.Balance -= field.Money;
        //    else
        //        bf.Balance += field.Money;
        //}
    }
}
